package com.ktjiaoyu.utils.fastjson2;

import com.alibaba.fastjson2.filter.ValueFilter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Set;

public class FastJson2DateValueFilter implements ValueFilter {
    private LinkedHashMap<String,String> dateMapping=null;

    public FastJson2DateValueFilter(LinkedHashMap<String,String> dateMapping){
        this.dateMapping=dateMapping;
    }
    @Override
    public Object apply(Object object, String s, Object value) {
        if (dateMapping==null){
            return value;
        }

        if (value instanceof Date){
            Set<String> keySet=dateMapping.keySet();
            for (String patten : keySet){
                if (s.matches(patten)){
                    String dateFormat=dateMapping.get(patten);

                    SimpleDateFormat format=new SimpleDateFormat(dateFormat);
                    return format.format((Date) value);
                }
            }
        }
        return value;
    }
}
